﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.DataService;
using System.IO.IsolatedStorage;
using System.IO;
using CommunitySystem.SystemService;
using CommunitySystem;

namespace EHR.Common
{
    public static class Session
    {
        public static string QueryString = "";
        private static UserInfo _LoginUser;
        private static TimeSpan _ClientServerTimeSpan;
        private static readonly string _ConfigFileName = "Setting.config";
        private static IsolatedStorageSettings appSettings = null;
        public  const string LOGINUSER = "loginUser";
        public static UserInfo LoginUser
        {
            get { return _LoginUser; }
            set
            {
                if (value != _LoginUser)
                {
                    if (appSettings == null)
                        appSettings = IsolatedStorageSettings.ApplicationSettings;
                    if (appSettings.Contains(LOGINUSER))
                        appSettings[LOGINUSER] = value;
                    else
                        appSettings.Add(LOGINUSER, value);
                    _LoginUser = value;
                    if (value != null)
                    {
                        Cookie.LastLoginName = _LoginUser.LoginName;
                        UpdateCookie();
                    }
                }
            }
        }
        //yanyan false为不用ikey，true为用ikey
        public static bool IsIKey = false;
        //public static bool IsLoginUserLowLevelEmp(int empId)
        //{
        //    foreach (LowLevelEmployeeInfo emp in LoginUser.LowLevelEmployees)
        //    {
        //        if (emp.EmpId == empId)
        //        {
        //            return true;
        //        }
        //    }

        //    return false;
        //}

        //public static bool IsLoginUserSubordinateOrg(int orgId)
        //{
        //    foreach (var i in LoginUser.SubordinateOrgs)
        //    {
        //        if (orgId == i)
        //        {
        //            return true;
        //        }
        //    }

        //    return false;
        //}

        public static string LastLoginName { get { return Cookie.LastLoginName; } }

        private static AppCookie Cookie = new AppCookie();

        public static DateTime LastActiveTime { get { return Cookie.LastActiveTime; } private set { Cookie.LastActiveTime = value; } }

        public static bool NeedPasswordDog = false;

        public static PassWordDog dog = new PassWordDog();

        public static void LoadCookie()
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite();
            if (isf.FileExists(_ConfigFileName))
            {
                using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(_ConfigFileName, FileMode.Open, isf))
                {
                    using (StreamReader reader = new StreamReader(fs))
                    {
                        string text = reader.ReadToEnd();
                        Cookie.FromString(text);
                    }
                }
            }
        }

       

        public static void Logout()
        {           
            SaveCookie(new DateTime(1, 1, 1));
            LoginUser = null;
            Session.dog.Password = string.Empty;
        }

        public static void UpdateCookie()
        {
            DateTime serverNow = DateTime.Now - _ClientServerTimeSpan;
            SaveCookie(serverNow);
        }

        private static void SaveCookie(DateTime lastActiveTime)
        {
            LastActiveTime = lastActiveTime;

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite();
            using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(_ConfigFileName, FileMode.OpenOrCreate, isf))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(Cookie.ToString());
                }
            }
        }
    }
}
